package com.aimaterials.resource.controller;

import java.util.List;

import com.aimaterials.common.core.domain.entity.SysUser;
import com.aimaterials.common.core.domain.model.LoginUser;
import com.aimaterials.common.utils.ServletUtils;
import com.aimaterials.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.aimaterials.common.annotation.Log;
import com.aimaterials.common.core.controller.BaseController;
import com.aimaterials.common.core.domain.AjaxResult;
import com.aimaterials.common.enums.BusinessType;
import com.aimaterials.resource.domain.ResCourse;
import com.aimaterials.resource.service.IResCourseService;
import com.aimaterials.common.utils.poi.ExcelUtil;
import com.aimaterials.common.core.page.TableDataInfo;

/**
 * 课程信息Controller
 * 
 * @author DaemonSu
 * @date 2020-11-04
 */
@RestController
@RequestMapping("/resource/course")
public class ResCourseController extends BaseController
{
    @Autowired
    private IResCourseService resCourseService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询课程信息列表
     */
    @PreAuthorize("@ss.hasPermi('resource:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResCourse resCourse)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        resCourse.setSchoolId(user.getDeptId());
        startPage();
        List<ResCourse> list = resCourseService.selectResCourseList(resCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程信息列表
     */
    @PreAuthorize("@ss.hasPermi('resource:course:export')")
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ResCourse resCourse)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        resCourse.setSchoolId(user.getDeptId());
        List<ResCourse> list = resCourseService.selectResCourseList(resCourse);
        ExcelUtil<ResCourse> util = new ExcelUtil<ResCourse>(ResCourse.class);
        return util.exportExcel(list, "course");
    }

    /**
     * 获取课程信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:course:query')")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") String courseId)
    {
        return AjaxResult.success(resCourseService.selectResCourseById(courseId));
    }

    /**
     * 新增课程信息
     */
    @PreAuthorize("@ss.hasPermi('resource:course:add')")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResCourse resCourse)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        resCourse.setSchoolId(user.getDeptId());
        return toAjax(resCourseService.insertResCourse(resCourse));
    }

    /**
     * 修改课程信息
     */
    @PreAuthorize("@ss.hasPermi('resource:course:edit')")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResCourse resCourse)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        resCourse.setSchoolId(user.getDeptId());
        return toAjax(resCourseService.updateResCourse(resCourse));
    }

    /**
     * 删除课程信息
     */
    @PreAuthorize("@ss.hasPermi('resource:course:remove')")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable String[] courseIds)
    {
        return toAjax(resCourseService.deleteResCourseByIds(courseIds));
    }
}
